<template>
  <!--  分配角色-->
  <a-modal
      title="分配角色"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      :destroyOnClose='true'
      cancel-text="取消"
      ok-text="确定"
      :scroll="{ x:true}"
  >
    <a-form
        :form="form"
        :label-col="{ span: 5 }"
        :wrapper-col="{ span: 12 }"
        @submit="handleOk"
    >
      <a-form-item label="人员姓名">
        <a-input
            allowClear
            placeholder="请输入人员姓名"
            v-decorator="['name', { rules: [{ required: true, message: '请输入人员姓名' }] }]"
        />
      </a-form-item>

      <a-form-item label="选择角色">
        <a-select
            allowClear
            mode="multiple"
            placeholder="请选择角色"
            v-decorator="['roleIds', { rules: [{ required: false, message: '请选择角色' }] }]"
        >
          <a-select-option
              v-for="item in jueseData"
              :key="item.id"
          >{{ item.name }}
          </a-select-option>
        </a-select>
      </a-form-item>

    </a-form>
  </a-modal>
</template>

<script>
import {adduser, getuserlist} from "@/api/structure";
import {saveRole} from "@/api/user";

export default {
  data() {
    return {
      visible: false,
      confirmLoading: false,
      jueseData: [],
      form: {
        id: '',
        name: '',
        roleIds: []
      },
      id: ''
    }
  },
  methods: {
    open(record) {
      console.log(record)
      this.id = record.id
      this.form = this.$form.createForm(this, {name: "", id: "", roleIds: []})
      this.$nextTick(() => {
        this.form.setFieldsValue({
          name: record.name,
          roleIds: record.roleIds.split(","),
        });
      });
      this.getjuese()
      this.visible = true
    },
    //获取角色列表
    async getjuese() {
      let res = await getuserlist();
      if (res.code == 0) {
        this.jueseData = res.data;
      }
    },
    //分配角色
    handleOk(e) {
      e.preventDefault();
      this.form.validateFields(async (err, values) => {
        if (!err) {
          let data;
          this.confirmLoading = true;
          data = {
            id: this.id,
            roleIds: values.roleIds.join(","),
          };
          let res = await saveRole(data);
          if (res.code === 0) {
            this.$message.success(res.msg)
            this.$emit('change')
          }
          this.confirmLoading = false;
          this.visible = false;
        }
      });
    },
    //取消弹窗
    handleCancel(e) {
      this.visible = false;
      this.visible1 = false;
      this.confirmLoading = false;
      this.yeshu1 = 1;
    },
  }
}
</script>

<style lang="less" scoped>

</style>